const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    underway: true,
    order_log: [{
        title: '设备发放',
        created_at: '2020-12-18 16:23:56',
      },
      {
        title: '项目配置',
        created_at: '2020-12-18 16:23:56',
        content: '孙医生'
      },
      {
        title: '设备押金返还',
        created_at: '2020-12-18 16:23:56',
      },
    ],
    presentStageArr: [
      '待处理',
      '进行中',
      '已完成',
      '已逾期'
    ],
    presentStageSubArr: [
      '此订单还未绑定设备。',
      '订单正在进行中。',
      '该订单已完成。',
      '该订单已逾期，请联系购买者尽快返还设备。'
    ],
    orderId: null,
    orderData: null,
    showDialogsd: false,
    hospInfo:null
  },
  onLoad(options) {
    let that = this
    that.setData({
      orderId: decodeURIComponent(options.codeId)
    })
    app.getHost(app.globalData.userInfo.hospitalId).then(res => {
      that.setData({
        hospInfo: res,
      })
    })
    this.getList()
  },

  hideDialog() {
    this.setData({
      showDialogsd: false
    })
},
  getList() {
    let that = this
    let newData = {
      context: this.data.orderId
    }
    wx.showLoading({
      title: '查询中...',
      mask: true
    })
    wx.request({
      url: app.globalData.serverUrl + '/ddOrder/detail/byQR',
      method: 'POST',
      header: {
        'token': wx.getStorageSync('token')
      },
      data: newData,
      success: (res => {
        let resData = res.data
        if (resData.code == 200) {
          wx.hideLoading()
          if(resData.data.order.presentStage==0){
            that.setData({
              orderData: resData.data.order
            })
          }else{
            that.showDialog({
            title: '无效二维码!',
            icon: 'fail',
            tip: ['该二维码已使用']
          })
          }
         
        } else {
          wx.hideLoading()
          that.showDialog({
            title: '无效二维码!',
            icon: 'fail',
            tip: [resData.message?resData.message:'请扫描已付款的自购服务订单二维码']
          })
        }
      })
    })
  },
  showDialog({
    title,
    tip,
    icon
  } = {}) {
    this.setData({
      title: title,
      tip: tip,
      icon: icon
    })
    this.selectComponent('#dialog').show()
  },
  colsFun(){
    wx.navigateBack({
      delta: 1,
    })
  },
  bindtapScan() {
    let that = this
    let newData = {}
    newData.orderId = that.data.orderData.orderId
    newData.orderNo = that.data.orderData.orderNo
    newData.username = that.data.orderData.username
    newData.phone = that.data.orderData.phone
    newData.treatments = that.data.orderData.treatments
    newData.projectOfDays = that.data.orderData.projectOfDays
    newData.treatmentOfDays = that.data.orderData.treatmentOfDays
    console.log(that.data.orderData,that.data.hospInfo);
    let sumHours =this.data.hospInfo.settlementUnit==2?(Number(that.data.orderData.projectOfDays)*Number(that.data.orderData.treatmentOfDays)):Number(that.data.orderData.projectOfDays)
    let initHours = this.data.hospInfo.settlementUnit==2?this.data.hospInfo.availableHours:this.data.hospInfo.availableDays
    if(initHours<sumHours && this.data.hospInfo.controlType == 0){
      that.setData({
        showDialogsd: true
      })
    }else{
      wx.navigateTo({
        url: '/pages/equipment/addzg/addzg?ovData=' + JSON.stringify(newData),
      })
    }
  }
})